/**
 * @author  guocailee
 */
import Vue from 'vue'
import md5 from 'js-md5'

export const updatePassword = (params) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/changepassword', {
      newPassword: md5(params.newPassword),
      password: md5(params.password)
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve(data.body)
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const resetPassword = (params) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/changepassword', {
      newPassword: md5(params.newPassword),
      username: params.username
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve(data.body)
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const updateEmail = (email) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/mailbind', {
      email: email
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const updateMobile = (mobile) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/mobilebind', {
      mobile: mobile
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 添加或更新用户密保（已经存在则更新密保）
 * @param  {Array} questionList []
 * @return {[type]}              [description]
 */
export const updateQeustion = (questionList) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/addorupdateusersecurity', {
      questionContent: questionList
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const getUserSecurityQuestion = () => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/getusersecurityquestion', {
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve(data.body.payload.results.results)
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const getQuestionList = () => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/findSecurityQuestion', {
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve(data.body.payload.results.paramLists)
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const resetPwdForget = (account) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/resetpwdforget', {
      username: account.username,
      newPassword: md5(account.newPassword)
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve(data.body.payload.results.paramLists)
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

